﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DirectUI.Mobile.UI.Controls;

namespace DirectUI.Mobile.UI
{
 public   class PageBuider
    {
     public PageBuider()
     {
     }
     public IList<Page> BuildPages(DirectoryInfo dir)
     {
         return null;
     }
     public  Page BuildPage(string fileName)
     {
         XmlDocument doc = new XmlDocument();
         doc.Load(fileName);
         return BuildPage(doc);
     }
     
     public Page BuildPageFromXml(string xml)
     {
         XmlDocument doc = new XmlDocument();
         doc.LoadXml(xml);
         return BuildPage(doc);
     }
     public Page BuildPage(XmlDocument doc)
     {
         try
         {
             IParser parser = GetParser(doc.DocumentElement);
             if (parser != null)
             {
                Page page= parser.Parse(doc.DocumentElement);
                page.OnLoad(null);
                return page;
             }
             return BuildErrorPage("Can't recognise root tag " + doc.DocumentElement.Name);
         }
         catch (System.Exception ex)
         {
             DirectUI.Common.Log.Instance.Error(ex.ToString());
           return  BuildErrorPage(ex.ToString());
         }
     }
     private Page BuildErrorPage(string msg)
     {
         Page page = new Page();
         page.BackColor = Color.Silver;
         DirectUILabel lb = new DirectUILabel();
         lb.Height = 400;
         lb.Width = 240;
         lb.Text = msg;
         page.Add(lb);
         return page;
     }
     private IParser GetParser(XmlNode node)
     {
         if (node.Name == "XPage")
         {
                 return    new  DirectUI.Mobile.UI.XPageParser();

         }
         return null;
     }
    }
}
